package com.project2.controller;

import com.project2.bean.ResultVo;
import com.project2.bean.bo.WxAddressDeleteBo;
import com.project2.bean.bo.WxAddressSaveBo;
import com.project2.bean.po.MarketAddress;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.DataInWxAddressDetailVo;
import com.project2.bean.vo.DataInWxAddressListVo;
import com.project2.service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangminghui
 * @date 2022/8/15
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public ResultVo list(HttpServletRequest httpServletRequest) {
        String header = httpServletRequest.getHeader("X-CskaoyanMarket-Token");
        MarketUser marketUser = (MarketUser) httpServletRequest.getSession().getAttribute(header);
        DataInWxAddressListVo data = null;
        if (marketUser != null) {
            // todo: 从session域中获取当前用户id
            Integer currentUserId = marketUser.getId();
            data = wxAddressService.queryDataInWxAddressListVo(currentUserId);
        }
        //DataInWxAddressListVo data = wxAddressService.queryDataInWxAddressListVo();
        return ResultVo.ok(data);
    }

    @RequestMapping("detail")
    public ResultVo detail(Integer id) {
        DataInWxAddressDetailVo data = wxAddressService.queryDataInWxAddressDetailVo(id);
        return ResultVo.ok(data);
    }

    @RequestMapping("save")
    public ResultVo save(@RequestBody WxAddressSaveBo wxAddressSaveBo, HttpServletRequest httpServletRequest) {
        //HttpSession session = httpServletRequest.getSession();
        //MarketUser marketUser = (MarketUser) session.getAttribute("X-CskaoyanMarket-Token");
        String header = httpServletRequest.getHeader("X-CskaoyanMarket-Token");
        MarketUser marketUser = (MarketUser) httpServletRequest.getSession().getAttribute(header);
        Integer data = 0;
        if (marketUser != null) {
            // todo: 从session域中获取当前用户id
            Integer currentUserId = marketUser.getId();

            // todo: 可以根据id字段判断是add还是update
            //if (wxAddressSaveBo.getIsDefault().equals(0) || wxAddressSaveBo.getIsDefault().equals(1)) {
            if (wxAddressSaveBo.getId().equals(0)) {
                // todo: 执行add，因为是新增，所以bo中携带的订单id始终为0
                // 如果isDefault字段为false，直接添加
                if (((Boolean) wxAddressSaveBo.getIsDefault()) == false) {
                    wxAddressService.saveAddressInfoByBo(wxAddressSaveBo, currentUserId);
                } else {
                    MarketAddress address = wxAddressService.queryAddressByisDefaultAndDeletedAndUserId(1, false, currentUserId);
                    if (address != null) {
                        wxAddressService.updateCurrentAddressInfoByIdAndisDefault(address.getId(), false);
                        wxAddressService.saveAddressInfoByBo(wxAddressSaveBo, currentUserId);
                    } else {
                        wxAddressService.saveAddressInfoByBo(wxAddressSaveBo, currentUserId);
                    }
                }
                //wxAddressService.saveAddressInfoByBo(wxAddressSaveBo, currentUserId);
            } else {
                // todo: 执行update，此时bo中的id为当前address在数据库表中的id，
                if (((Boolean) wxAddressSaveBo.getIsDefault()) == false) {
                    wxAddressService.updateAddressInfoByBo(wxAddressSaveBo);
                } else {
                    //MarketAddress address = wxAddressService.queryAddressByisDefaultAndDeleted(true, false);
                    //MarketAddress address = wxAddressService.queryAddressByisDefaultAndDeletedAndUserId(true, false, currentUserId);
                    MarketAddress address = wxAddressService.queryAddressByisDefaultAndDeletedAndUserId(1, false, currentUserId);
                    if (address != null) {
                        wxAddressService.updateCurrentAddressInfoByIdAndisDefault(address.getId(), false);
                        wxAddressService.updateAddressInfoByBo(wxAddressSaveBo);
                    } else {

                        wxAddressService.updateAddressInfoByBo(wxAddressSaveBo);
                    }
                }
                //wxAddressService.updateAddressInfoByBo(wxAddressSaveBo);
            }
            //Integer data = wxAddressService.queryDataInWxAddressSaveVo(wxAddressSaveBo);
            data = wxAddressService.queryDataInWxAddressSaveVo(wxAddressSaveBo, currentUserId);
        }
        //Integer data = wxAddressService.queryDataInWxAddressSaveVo(wxAddressSaveBo, currentUserId);
        return ResultVo.ok(data);
    }

    @RequestMapping("delete")
    public ResultVo delete(@RequestBody WxAddressDeleteBo wxAddressDeleteBo) {
        wxAddressService.deleteAddressInfoById(wxAddressDeleteBo.getId());
        return ResultVo.ok();
    }

}
